home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 2 / Gold Medal Software Volume 2 (Gold Medal) (1994).iso / prog / asm_n_z.arj / SD2.ASM < prev    next >
Assembly Source File  |  1987-05-15  |  15KB  |  412 lines

  1. ;------------------------------------------------------------------------------;
  2. ;                                                                              ;
  3. ;                                                                              ;
  4. ;                                                                              ;
  5. ; Switch Directory - SD                                                        ;
  6. ;                                                                              ;
  7. ;                                                                              ;
  8. ;                                                                              ;
  9. ;Usage                                                                         ;
  10. ;                                                                              ;
  11. ;   [d:\....]>SD [drive][switch][subdirectory name]                            ;
  12. ;                                                                              ;
  13. ;                        [drive] - the drive to search (if not searhing        ;
  14. ;                                  current drive)                              ;
  15. ;                                                                              ;
  16. ;                        [switch] = blank - search whole disk                  ;
  17. ;                                                                              ;
  18. ;                                 = \ - search only subdirectories of the      ;
  19. ;                                       current directory                      ;
  20. ;                                                                              ;
  21. ;                                 = / - search only one path deep. Searches    ;
  22. ;                                       the root directory only. (Similar      ;
  23. ;                                       to DOS CD command with one pathname)   ;
  24. ;                                                                              ;
  25. ;                                                                              ;
  26. ;                                                                              ;
  27. ;                        [subdirectory name] - the name of an individual       ;
  28. ;                                              subdirectory (not the           ;
  29. ;                                              complete path)                  ;
  30. ;                                                                              ;
  31. ;                        If the command line is left blank then you are        ;
  32. ;                        taken to the root directory of the disk               ;
  33. ;                                                                              ;
  34. ;                                                                              ;
  35. ;Sorce code heavily dependent on Vern Buerg's LDIR program. Thanks Vern        ;
  36. ;                                                                              ;
  37. ;3/11/87 - Stephen Falatko                                                     ;
  38. ;                                                                              ;
  39. ;Written for the A86 assembler                                                 ;
  40. ;                                                                              ;
  41. ;------------------------------------------------------------------------------;
  42.       ;    Simple change directory
  43.  
  44. CODE SEGMENT
  45.  
  46.       Org    0100
  47.  
  48. Stackx       Dw      0                       ;Entry stack pointer
  49.  
  50.       Mov    Stackx,SP               ;Save stack ptr for exiting
  51.       Jmp    Start
  52. ;
  53. ;     Data Areas, Constants, Etc.
  54.  
  55. LF           Equ     10
  56. CR           Equ     13
  57. Stopper      Equ     255                    ;Ends print strings
  58.  
  59. CURDSK       Equ     019                     ;Get current disk
  60. SETDTA       Equ     01A                     ;Set data transfer area
  61. CHDIR        Equ     03B                     ;Change directory
  62. GETPATH      Equ     047                     ;Get current directory
  63.  
  64. Errlvl       Db      0                       ;DOS return code
  65.  
  66. RootFlag     Db      0
  67. OneDeepFlag  Db      0
  68.  
  69. Sub_Dir      Db      16 Dup (0)              ;The sub dir we want to change to
  70.  
  71. Done_Flag    Dw      0
  72.  
  73. Count        Dw      0                       ;Number of args on command line
  74.  
  75. DtaPointer   Dw      DtaAreaBegin
  76. Direction    Db      0
  77. BackOneDir   Db      '..',0
  78. SearchAsciiZ Db      '*.*',0
  79.  
  80. ;
  81. ;     Headings and titles
  82.  
  83. Not_Found_Msg  Db  CR,LF,'Subdirectory Not Found',CR,LF,Stopper
  84. PathErrorMsg   Db  CR,LF,'Illegal \ charater in path name',CR,LF,Stopper
  85.  
  86. Help           Db  CR,LF,'Usage:',CR,LF,CR,LF
  87.                Db  '[d:\....]>SD [drive][switch][subdirectory name]',CR,LF
  88.                Db  '      [drive] - the drive to search. (if not searching',CR,LF
  89.                Db  '                current drive)',CR,LF,CR,LF
  90.                Db  '      [switch] = blank - search whole disk',CR,LF,CR,LF
  91.                Db  '               = \ - search only subdirectories of the',CR,LF
  92.                Db  '                     current directory',CR,LF,CR,LF
  93.                Db  '               = / - search only one path deep. Searches',CR,LF
  94.                Db  '                     the root directory only. (Similar',CR,LF
  95.                Db  '                     to DOS CD command with one pathname)',CR,LF,CR,LF
  96.                Db  '      [subdirectory name] - the name of an individual',CR,LF
  97.                Db  '                            subdirectory (not the',CR,LF
  98.                Db  '                            complete path)',CR,LF
  99.                Db  '      If the command line is left blank then you are',CR,LF
  100.                Db  '      taken to the root directory of the disk',CR,LF,Stopper
  101.  
  102. OrigDr       Db      'x:'                    ;Original drive
  103. OrigDir      Db      '\',63 Dup (0)          ; and path
  104.  
  105. RootDir      Db      'x:\',0                 ;To get vol label
  106.  
  107.  
  108. ;
  109. ;     Set default drive and path
  110.  
  111. Start:
  112.       Mov    AH,0D                   ; Reset diskettes
  113.       Int    021
  114.  
  115.       Mov    AH,CURDSK               ; Get current disk
  116.       Int    021
  117.       Add    AL,'A'
  118.       Mov    OrigDr,AL               ; Save original drive letter
  119.       Mov    RootDir,AL              ;
  120.  
  121.       Mov    AH,GETPATH              ; Save original path
  122.       Mov    DL,OrigDr
  123.       Sub    DL,'@'                  ; a little monkey business to set
  124.       Mov    SI,Offset OrigDir + 1   ; the original drive
  125.       Int    021
  126.  
  127.       Mov    AX,02523                ; set Ctrl+Break vector to point
  128.       Mov    DX,Offset NFCB          ; to our not found. This way a Ctrl+Brk
  129.       Int    021                     ; will leave us in the place we started
  130.  
  131.       Sub    CX,CX                   ; Clear CX
  132.       Mov    CL,B [080]              ; Get the number of characters in
  133.  
  134.       Cmp    CX,0                    ; Anything?
  135.       Jne    L1
  136.       Call No_Arg                    ;set root dir and leave
  137.       Jmp    Exit
  138.   L1:
  139.       Mov    SI,081                  ; set up to parse command line
  140.       Mov    DI,Offset Sub_dir
  141.  
  142.       Push   DI
  143.       Mov    DI,SI
  144.       Mov    al,':'                  ;drive specifier present?
  145.       Repne  Scasb
  146.       Cmp    B [DI-1],':'
  147.       Jne    L10
  148.       Mov    SI,DI                   ;point SI to character following :
  149.       Sub    DI,2                    ;point DI to drive letter
  150.       Mov    AL,B [DI]
  151.       Xor    AL,020
  152.       Mov    RootDir,AL
  153.  
  154.       Sub    DX,DX
  155.       Mov    AH,0E                   ; set drive
  156.       Mov    DL,AL
  157.       Sub    DL,'A'
  158.       Int    021
  159.  
  160.       Cmp    B [SI],CR
  161.       If e Call No_Arg
  162.  
  163.       Pop    DI
  164.       Jmp Short L2
  165.   L10:
  166.       Pop    DI
  167.   L2:
  168.       Lodsb
  169.  
  170.       Cmp    AL,' '                  ;strip leading blanks
  171.       Jz     L2
  172.  
  173.       Cmp    AL,0D                   ;carriage return
  174.       Jz     L7
  175.  
  176.       Cmp    AL,'?'                  ;?
  177.       Jne    L3
  178.       Cmp    DI,Offset Sub_Dir       ;still pointing to beginning?
  179.       Jne    L3
  180.       Mov    DX,Offset Help
  181.       Call PrintS
  182.       Jmp    Exit                    ;load next
  183.   L3:
  184.       Cmp    AL,'/'                  ;/ ?
  185.       Jne    L30
  186.       Cmp    DI,Offset Sub_Dir       ;still pointing to beginning?
  187.       Jne    L30
  188.       Mov    OneDeepFlag,1           ;set flag so we only search one deep
  189.       Jmp Short L2                   ;load next
  190.   L30:
  191.       Cmp    AL,'\'                  ;\ ?
  192.       Jne    L4
  193.       Cmp    DI,Offset Sub_Dir       ;still pointing to beginning?
  194.       Jne    L31
  195.       Mov    RootFlag,1              ;set flag so we won't set to root
  196.       Jmp Short L2                   ;load next
  197.   L31:
  198.       Jmp Short PathError            ;nope, ERROR!
  199.   L4:
  200.       Cmp    RootFlag,1              ;been here before?
  201.       Je     L5                      ;yep, go on
  202.       Push   AX
  203.       Call No_Arg                    ;no, set to root directory
  204.       Pop    Ax
  205.       Mov    RootFlag,1              ;set flag
  206.   L5:
  207.       Cmp    AL,'0'                  ;compare with 0
  208.       Jb     L2                      ;get next char if smaller
  209.  
  210.       Cmp    AL,'z'                  ;compare with z
  211.       Ja     L2                      ;get next char if bigger
  212.  
  213.       Cmp    AL,'a'                  ;lowercase letter?
  214.       Jb     L6                      ;nope so go on
  215.       Xor    AL,020                  ;make upper case
  216.   L6:
  217.       Stosb
  218.       Jmp Short L2
  219.   L7:
  220.       Mov    Count,DI                ;how many characters stored?
  221.       Sub    Count,Offset Sub_Dir
  222.       Cmp    Count,0
  223.       Je     Exit
  224.  
  225.       Mov    AL,0
  226.       Stosb
  227.  
  228.       Call   GetDir                  ; Read the directory
  229.  
  230.       Cmp    Done_Flag,1
  231.       Jne    Not_Found
  232.  
  233.       Jmp Short Exit
  234.  
  235.  
  236. Not_Found:
  237.  
  238.       Mov    DX,Offset Not_Found_Msg
  239.       Call   PrintS
  240.   NFCB:
  241.       Sub    DX,DX
  242.       Mov    DL,OrigDr
  243.       Cmp    DL,RootDir
  244.       Je     NF1
  245.  
  246.       Mov    AH,0E                   ; set drive
  247.       Sub    DL,'A'
  248.       Int    021
  249.    NF1:
  250.       Mov    AH,CHDIR                ;Set path to original path
  251.       Mov    DX,Offset OrigDr
  252.       Int    021
  253.  
  254.  
  255.       Jmp Short Exit
  256.  
  257. PathError:
  258.       Mov    DX,Offset PathErrorMsg
  259.       Call   PrintS
  260.  
  261. Exit:
  262.         Mov     SP,Stackx               ;Insure exiting stack
  263.  
  264. Done:        Mov     AL,Errlvl               ;Return to system
  265.       Mov    AH,04C                  ; via EXIT
  266.       Int    021
  267. ;
  268. ;
  269. ;     Start at Root Directory
  270.  
  271. No_Arg:
  272.                                      ; If no argument then set current
  273.       Mov    AH,CHDIR                ; path to root directory
  274.       Mov    DX,Offset RootDir
  275.       Int    021
  276.  
  277.       Ret
  278.  
  279. ;
  280. ;
  281. ;     Search for matching sub directory
  282.  
  283. GetDir:
  284.  
  285.           Mov     Done_Flag,0
  286.  
  287.  
  288. ;     Find first or next subdirectory level
  289. ;     -------------------------------------
  290.  
  291. NextLevel:
  292.           Mov     DX,[DTAPointer]      ; Next nested DTA
  293.           Mov     AH,1Ah               ; For DOS call to set DTA
  294.           Int     21h                  ; Do it
  295.  
  296.           Cmp     [Direction],0        ; Check if we're nesting
  297.           Jnz     FindNextFile         ; If not, we're continuing
  298.  
  299.           Mov     DX,Offset SearchAsciiZ     ; We search for *.*
  300.           Mov     CX,10h               ; Subdirectory attribute
  301.           Mov     AH,4Eh               ; Find first file
  302.           Int     21h                  ;   by calling DOS
  303.  
  304.           Jmp     Short TestMatch      ; Hop around next section
  305. FindNextFile:
  306.           Mov     AH,4Fh               ; Find next file
  307.           Int     21h                  ;   by calling DOS
  308. TestMatch:
  309.           Jc      NoMoreFiles          ; If CY flag, at end of rope
  310.  
  311.           Mov     BX,[DTAPointer]      ; Our find stuff is here
  312.           Test    B [BX + 21],10h      ; Test if directory attribute
  313.           Jz      FindNextFile         ; If not, continue search
  314.  
  315.           Add     BX,30                ; Now points to directory name
  316.           Cmp     Byte Ptr [BX],'.'    ; Ignore "." and ".." entries
  317.           Jz      FindNextFile         ;   by continuing the search
  318.  
  319.           Cmp     OneDeepFlag,1        ; looking only at this dir?
  320.           Je      Compare
  321.  
  322.           Push    BX                   ; save pointer to subdir name
  323.  
  324.           Mov     DX,BX                ; Now DX points to found dir
  325.           Mov     AH,3Bh               ; Set up DOS function call
  326.           Int     21h                  ; And change directory
  327.  
  328.           Pop     BX                   ; get pointer to subdir name back
  329.     Compare:
  330.           Push    CX                   ;check to see if its the path we want
  331.           Push    SI
  332.           Push    DI
  333.           Sub     CX,CX
  334.           Mov     CX,Count
  335.           Mov     DI, Offset Sub_Dir
  336.           Lea     SI, BX
  337.           Repe    Cmpsb
  338.  
  339.           Pop     DI
  340.           Pop     SI
  341.           Pop     CX
  342.  
  343.           Jz      Found                ; matched up so leave
  344.  
  345.           Cmp     OneDeepFlag,1
  346.           Jne     GoOn
  347.           Mov     [Direction],-1
  348.           Jmp Short NextLevel
  349.       GoOn:
  350.           Add     [DtaPointer],43      ; New DTA for new level
  351.           Mov     [Direction],0        ; I.E., Find first file
  352.  
  353.           Jmp     NextLevel            ; All ready to cycle through
  354.  
  355. ;     No More Files Found -- go back to previous level
  356. ;     ------------------------------------------------
  357.  
  358. NoMoreFiles:
  359.           Cmp     [DTAPointer],Offset DtaAreaBegin   ; See if back at start
  360.  
  361.           Jz      ExitGD               ; If so, that's all, folks
  362.  
  363.           Sub     [DTAPointer],43      ; Back one for previous
  364.           Mov     [Direction],-1       ; I.E., will find next file
  365.  
  366.           Mov     DX,Offset BackOneDir ; The string ".."
  367.           Mov     AH,3Bh               ; Call to change directory
  368.           Int     21h                  ; Change directory to father
  369.  
  370.           Jmp     NextLevel            ; And continue the search
  371. Found:
  372.           Cmp     OneDeepFlag,1
  373.           If ne Jmp Short F1
  374.  
  375.           Mov     DX,BX                ; Now DX points to found dir
  376.           Mov     AH,3Bh               ; Set up DOS function call
  377.           Int     21h                  ; And change directory
  378.       F1:
  379.           Mov     Done_Flag,1
  380. ExitGD:
  381.           Ret
  382.  
  383. ;
  384. ;
  385. ;     Print String like INT 21H function 9
  386.  
  387. PrintS:                                      ; DX has offset to string
  388.       Push   SI                      ;  ending in char x'FF'
  389.       Push   BX
  390.       Push   CX
  391.       Mov    SI,DX                   ; Ptr to string text
  392.       Sub    CX,CX                   ; Overall text length
  393. PS1:  Lodsb
  394.       Cmp    AL,Stopper              ; Ending hex FF?
  395.       Je     PS9
  396.       Inc    CX
  397.       Jmp    Short PS1
  398.  
  399. PS9:
  400.       Mov    BX,1                    ; Standard output device
  401.       Mov    AH,40h                  ;  to write to
  402.       Int    21h
  403.  
  404.       Pop    CX                      ; Recover registers
  405.       Pop    BX
  406.       Pop    SI
  407.       Ret
  408.  
  409.  
  410. DtaAreaBegin   equ     $                          ;   is also the DTA area
  411. DtaAreaEnd     equ     DtaAreaBegin + 32 * 43     ; Can have 32 DTAs of 43 bytes
  412.